package com.asset.inventory.view

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.asset.inventory.BR
import com.asset.inventory.R
import com.asset.inventory.adapter.ConfigInfoAdapter
import com.asset.inventory.base.BaseActivity
import com.asset.inventory.constant.PageType
import com.asset.inventory.databinding.ActivityConfiginfoBinding
import com.asset.inventory.model.ConfigInfo
import com.asset.inventory.viewModel.ConfigInfoViewModel

/**
 * 页面名称：配置信息
 * 创建时间  2021/04/11 21:06
 */
public class ConfigInfoActivity: BaseActivity<ConfigInfoViewModel,ActivityConfiginfoBinding>() {

    private val mConfigList = mutableListOf<ConfigInfo>()

    private val adapter: ConfigInfoAdapter by lazy{
        ConfigInfoAdapter(mConfigList){
            val intent = Intent()
            val bundle = Bundle()
            bundle.putSerializable("CONFIG_INFO",mConfigList[it])
            intent.putExtras(bundle)
            setResult(RESULT_OK,intent)
            finish()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_configinfo

    override fun initVariableId(): Int = BR.configInfoViewModel

    override fun initViewModel(): Class<ConfigInfoViewModel> = ConfigInfoViewModel::class.java

    override fun initView() {
        val title: TextView = binding.titlebar.findViewById(R.id.title)
        val back: TextView = binding.titlebar.findViewById(R.id.left)
        val exit: TextView = binding.titlebar.findViewById(R.id.right)
        exit.visibility = View.INVISIBLE
        back.visibility = View.VISIBLE
        back.text = "返回"
        back.setOnClickListener {
            finish()
        }
        val type = intent.extras?.getInt("PAGE_TYPE")
        val code = intent.extras?.getString("CODE")
        when(type){
            PageType.TYPE_DEPARTMENT ->{
                title.text = "使用部门"
                mViewModel.getConfigInfoList(type)
            }
            PageType.TYPE_USER -> {
                title.text = "使用人"
                code?.let { mViewModel.getConfigInfoList(type, it) }
            }
            PageType.TYPE_LOCATION ->{
                title.text = "位置"
                mViewModel.getConfigInfoList(type)
            }
        }
        binding.rvList.layoutManager = LinearLayoutManager(this)
        binding.rvList.adapter = adapter
    }

    override fun initListener() {
        mViewModel.observerConfigInfo().observe(this, Observer {
            if(mConfigList.size > 0) mConfigList.clear()
            mConfigList.addAll(it)
            adapter.notifyDataSetChanged()
        })
    }
}